/**
 * File: Course.java
 * Author: Jonathan Spungen
 * Date: 04/24/2011
 *  
 */
package virtualadvisor;

import java.util.List;

/**
 * The Course class. Holds information about a course.
 */
public class Course {

	private String name;  // The course name.
	private String dept;  // The deparement.
	private int number;   // The course number.
	private String title; // The course title
	private String desc;  // The course description.
	private char grade;   // The course grade.
	private int credits;  // The number of credits.
	private String notes; // Notes on the course.
	private List<String> prereqs;  // The list of prerequisite courses.
	private boolean gep = false;   // Fulfills a GEP requirement.
	private List<String> GEPReq;   // The GEP requirement fulfilled.

	public Course(String name, String dept, int number, String title,
			String desc, char grade, int credits, List<String> prereqs,
			boolean gep, List<String> gEPReq) {
		super();
		this.name = name;
		this.dept = dept;
		this.number = number;
		this.title = title;
		this.desc = desc;
		this.grade = grade;
		this.credits = credits;
		this.prereqs = prereqs;
		this.gep = gep;
		this.GEPReq = gEPReq;
		this.notes = "";
	}
	
	public Course(String name, char grade, String notes){
		this.name = name;
		this.grade = grade;
		this.notes = notes;
	}

	/**
	 * Basic test for equality based on the name.
	 * 
	 * @param comp
	 *            the course to be compared
	 * @return true, if successful
	 */
	public boolean equals(Object comp) {
		Course c = (Course) comp;
		if (c.name.equalsIgnoreCase(name))
			return true;
		else
			return false;
	}

	/**
	 * Gets the credits.
	 * 
	 * @return the credits
	 */
	public int getCredits() { return credits; }

	/**
	 * Gets the department.
	 * 
	 * @return the department
	 */
	public String getDept() { return dept; }

	/**
	 * Gets the description.
	 * 
	 * @return the description
	 */
	public String getDesc() { return desc; }

	/**
	 * Getter for gEPreq.
	 * 
	 * @return the gEPreq
	 */
	public List<String> getGEPReq() {
		if (!gep)
			return null;
		else
			return GEPReq;
	}

	public char getGrade() { return grade; }

	/**
	 * Gets the name.
	 * 
	 * @return the name
	 */
	public String getName() { return name; }

	/**
	 * Gets the course number.
	 * 
	 * @return the number
	 */
	public int getNumber() { return number; }

	public List<String> getPrereqs() { return prereqs; }

	/**
	 * Gets the title.
	 * 
	 * @return the title
	 */
	public String getTitle() { return title; }

	/**
	 * Checks if is a GEP req.
	 * 
	 * @return true, if is GEP
	 */
	public boolean isGEP() { return gep; }

	public void setCredits(int credits) { this.credits = credits; }

	/**
	 * Sets the department.
	 * 
	 * @param dept
	 *            the new department
	 */
	public void setDept(String dept) { this.dept = dept; }

	/**
	 * Sets the description.
	 * 
	 * @param desc
	 *            the new description
	 */
	public void setDesc(String desc) { this.desc = desc; }

	/**
	 * Sets the GEP boolean.
	 * 
	 * @param gep
	 *            the new gep
	 */
	public void setGep(boolean gep) { this.gep = gep; }

	/**
	 * Sets the GEP requirement.
	 * 
	 * @param gEPReq
	 *            the new gEP requirement
	 */
	public void setGEPReq(List<String> gEPReq) { GEPReq = gEPReq; }

	public void setGrade(char grade) { this.grade = grade; }

	/**
	 * Sets the name.
	 * 
	 * @param name
	 *            the new name
	 */
	public void setName(String name) { this.name = name; }

	/**
	 * Sets the number.
	 * 
	 * @param number
	 *            the new course number
	 */
	public void setNumber(int number) { this.number = number; }

	public void setPrereqs(List<String> prereqs) { this.prereqs = prereqs; }


	

	/**
	 * Sets the title.
	 * 
	 * @param title
	 *            the new title
	 */
	public void setTitle(String title) { this.title = title; }
	
	/**
	 * Name: setNotes
	 * Precondition: None
	 * Postcondition: Notes on the course is updated
	 */
	public void setNotes(String notes) { this.notes = notes; }
	
	/**
	 * Name: getNotes
	 * Precondition: None
	 * Postcondition: None
	 * @return - Notes of the course
	 */
	public String getNotes() { return notes; }

	/**
	 * Name: toString() 
	 * Precondition: None
	 * Postcondition: Gathers information on the class to be displayed
	 * @return - Output on the class to be displayed
	 */
	public String toString(){
		int length = 125 - title.length();
		String num = rightPad(Integer.toString(number), 25);
		String tit = rightPad(title, length);
		return String.format("%s %s %s %d%n ", dept.trim(), num, tit, credits);
	}	

    private static String rightPad(String s, int width) {
        return String.format("%-" + width + "s", s).replace(' ', ' ');
    }

	public String descToString(){
		String text = "";
		
		text = dept+ number + "  -  " + title + "\n\n";
		text = text + desc ;
		
		if(prereqs != null){
			text = text + "\n\nPrerequisite: ";
			for(String temp: prereqs)
				text = text + temp + ", ";
		}

		if(GEPReq != null){
			text = text + "\n\nGEP: ";
			for(String temp: GEPReq)
				text = text + temp + ", ";
		}
		text = text + "\n\n";
		text = text + "Credits: " + credits + "\n";

		return text;
	}

	/**
	 * Name: XMLCoderXMLCoderCourse
	 * Precondition: None
	 * Postcondition: Gathers information on class to be displayed 
	 *              in XML format for saving to file.
	 * @return - Output on the class to be displayed in XML format
	 */
	public String XMLCoderCourse() {
		String xml = "";
		
		xml = "  <course id=\"" + name + "\">\n";
		xml = xml + "   <title>" + title + "</title>\n";
		xml = xml + "   <dept>" + dept + "</dept>\n";
		xml = xml + "   <number>" + number + "</number>\n";
		xml = xml + "   <description>" + desc + "</description>\n";
		
		if(prereqs != null)
			for(int x = 0; x < prereqs.size(); x++)
				xml = xml + "   <prereq>" + prereqs.get(x) + "</prereq>\n";
		
		if(GEPReq != null)
			for(int x = 0; x < GEPReq.size(); x++)
				xml = xml + "   <gep>" + GEPReq.get(x) + "</gep>\n";
		
		xml = xml + "   <credits>" + credits + "</credits>\n";
	    xml = xml + "   </course>\n";

		return xml;
	}
	
	/**
	 * Name: XMLCoder
	 * Precondition: None
	 * Postcondition: Gathers class information for degree progress xml 
	 * @return - Output on the class to be displayed in XML format
	 */
	public String XMLCoder() {
		String xml = "";
		
		xml = "   <course id=\"" + name + "\">\n";
		xml = xml + "    <grade>" + grade + "</grade>\n";
		xml = xml + "    <notes>" + notes + "</notes>\n";
		xml = xml + "    </course>\n";
		
		return xml;
	}
}

